package controls;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pages.WebPage;
import reports.Report;
import utils.Events;

public class TextField {

	private WebElement textField;
	private By by;
	String txtDescription;
	private Events events;

	/**
	 * This method will return the By of the text field
	 * 
	 * @author Pradeep Sundaram
	 * @return
	 */
	public By getBy() {
		return by;
	}

	/**
	 * This method will return the webElement of the text field
	 * 
	 * @author Pradeep Sundaram
	 * @return
	 */
	public WebElement getWebElement() {
		return textField;
	}

	/**
	 * Text field Constructor
	 * 
	 * @author Pradeep Sundaram
	 * @param textID
	 * @param fieldDesc
	 * @param util
	 * @param events
	 */
	public TextField(String textID, String fieldDesc, ElementFinder util,
			Events events) {
		this.events = events;
		if (textID.startsWith("id")) {
			textField = util.findElementByID(textID);
		} else if (textID.startsWith("name")) {
			textField = util.findElementByName(textID);
		} else if (textID.startsWith("css")) {
			textField = util.findElementByCss(textID);
		} else if (textID.startsWith("//")) {
			textField = util.findElementByXpath(textID);
		}
		WebPage.elementList.put(textField, fieldDesc);
	}

	/**
	 * Text field Constructor with By
	 * 
	 * @param textID
	 * @param byOfTf
	 * @param fieldDesc
	 * @param util
	 * @param events
	 */
	public TextField(String textID, By byOfTf, String fieldDesc,
			ElementFinder util, Events events) {
		this.events = events;
		if (textID.startsWith("id")) {
			textField = util.findElementByID(textID);
		} else if (textID.startsWith("name")) {
			textField = util.findElementByName(textID);
		} else if (textID.startsWith("css")) {
			textField = util.findElementByCss(textID);
		}
		by = byOfTf;
		WebPage.elementList.put(textField, fieldDesc);
	}

	/**
	 * This method will type the text in Text Field
	 * 
	 * @author Pradeep Sundaram
	 * @param text
	 */
	public void type(String text) {
		events.type(textField, text);
	}

	/**
	 * This method will click in the Text Field for Date controls
	 * 
	 * @author Pradeep Sundaram
	 */
	public void click() {
		events.click(textField);
	}

	/**
	 * this method will double click on the text
	 * 
	 * @author Pradeep Sundaram
	 */
	public void doubleClick() {
		events.doubleClick(textField);
	}

	/**
	 * This method will return the text in the text field
	 * 
	 * @author Pradeep Sundaram
	 * @return String
	 */
	public String getText() {
		return textField.getAttribute("value");
	}

	/**
	 * will return boolean based on the presence of the text field
	 * 
	 * @return boolean
	 */
	public boolean isDisplayed() {
		return textField.isDisplayed();
	}

	public void setTextField(WebElement textField) {
		this.textField = textField;
	}

	/**
	 * This method will return the tool tip of the Text Field
	 * 
	 * @author PSubramani33
	 * @return String
	 */
	public String getToolTip() {
		Report.log("Getting the tool tip of the button "
				+ WebPage.elementList.get(textField) + ".<BR>");
		return textField.getAttribute("title");
	}
}
